local BlockInputField = import("../BlockInputField.lua");
local Field = commonlib.inherit(BlockInputField);

local MinEditFieldWidth = 120;
-- local TextMarginUnitCount = Const.TextMarginUnitCount;    -- 文本边距

Field:Property("Value");    -- 字段值
Field:Property("Text");     -- 显示的文本值

function Field:ctor()
end

function Field:Init(block, opt)
    Field._super.Init(self, block, opt);

    self:SetValue(opt.value or "");

    return self;
end

function Field:UpdateWidthHeightUnitCount()
    local widthUnitCount = self:GetTextWidthUnitCount(self:GetLabel()) + (TextMarginUnitCount + Const.BlockEdgeWidthUnitCount) * 2;
    return math.min(math.max(widthUnitCount, Const.MinTextShowWidthUnitCount), Const.MaxTextShowWidthUnitCount), Const.LineHeightUnitCount;
end

function Field:IsField()
    return true;
end

function Field:IsCanEdit()
    return true;
end

function Field:IsEditRender()
    return false;
end

function Field:GetBlockly()
    return self:GetBlock():GetBlockly();
end

function Field:GetInputFieldValue()
    local value = self:GetValue();
    return value;
    -- if (self:IsNumberType()) then
    --     return self:GetNumberValue();
    -- elseif (self:IsCodeType()) then
    --     return value ~= "" and value or "nil"; 
    -- else 
    --     return self:GetValue();
    -- end
end

-- 获取xmlNode
function Field:SaveToXmlNode()
    local field_name = self:GetName();
    if (not field_name or field_name == "") then return nil end
    
    return {
        name = "field", 
        attr = {name = field_name}, 
        -- value
        [1] = {
            name = "value",
            -- text
            [1] = {
                text = self:GetValue(), 
                cdata = true,
            },
        },
    };
end

function Field:LoadFromXmlNode(xmlnode)
    local value = "";
    for _, subXmlNode in ipairs(xmlnode) do
        if (subXmlNode.name == "value") then
            for _, textXmlNode in ipairs(subXmlNode) do
                value = value .. textXmlNode.text; 
            end
        end
    end

    self:SetValue(value);
end


return Field;